package com.cskaoyan.servlet;


import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.BrandVo;
import com.cskaoyan.service.AdminBrandService;
import com.cskaoyan.service.AdminBrandServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/brand/*")
public class AdminBrandServlet extends WdBaseServlet {

    AdminBrandService adminBrandService = new AdminBrandServiceImpl();

    public void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        String order = request.getParameter("order");
        String sort = request.getParameter("sort");
        String username = request.getParameter("name");
        Integer id = null;
        if (request.getParameter("id") == null || request.getParameter("id").equals("")) {
            id = null;
        } else {
            id = Integer.parseInt(request.getParameter("id"));
        }

        CommonData<MarketBrand> data = adminBrandService.query(page, limit, sort, order, username, id);
        ResponseUtil.responseJson(response, BaseRespVo.ok(data));
    }

    public BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String str = request.getReader().readLine();
        MarketBrand marketBrand = JacksonUtil.read(str, MarketBrand.class);

        BaseRespVo baseRespVo = adminBrandService.delete(marketBrand);
        return baseRespVo;
    }

    public BaseRespVo update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String str = request.getReader().readLine();
        MarketBrand marketBrand = JacksonUtil.read(str, MarketBrand.class);

        BaseRespVo baseRespVo = adminBrandService.update(marketBrand);
        return baseRespVo;
    }


    private void create(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        String jsonStr = request.getReader().readLine();
        BrandVo brandVo = JacksonUtil.read(jsonStr, BrandVo.class);

        ResponseUtil.responseJson(response,adminBrandService.create(brandVo));

    }

}
